{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Input and Output"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from __future__ import print_function\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "author = \"kyubyong. https://github.com/Kyubyong/numpy_exercises\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'1.12.0'"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.__version__"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2017-04-01\n"
     ]
    }
   ],
   "source": [
    "from datetime import date\n",
    "print(date.today())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## NumPy binary files (NPY, NPZ)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Q1. Save x into `temp.npy` and load it."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n"
     ]
    }
   ],
   "source": [
    "x = np.arange(10)\n",
    "np.save('temp.npy', x) # Actually you can omit the extension. If so, it will be added automatically.\n",
    "\n",
    "# Check if there exists the 'temp.npy' file.\n",
    "import os\n",
    "if os.path.exists('temp.npy'):\n",
    "    x2 = np.load('temp.npy')\n",
    "    print(np.array_equal(x, x2))\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Q2. Save x and y into a single file 'temp.npz' and load it."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "True\n"
     ]
    }
   ],
   "source": [
    "x = np.arange(10)\n",
    "y = np.arange(11, 20)\n",
    "np.savez('temp.npz', x=x, y=y)\n",
    "# np.savez_compressed('temp.npz', x=x, y=y) # If you want to save x and y into a single file in compressed .npz format.\n",
    "with np.load('temp.npz') as data:\n",
    "    x2 = data['x']\n",
    "    y2 = data['y']\n",
    "    print(np.array_equal(x, x2))\n",
    "    print(np.array_equal(y, y2))\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Text files"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Q3. Save x to 'temp.txt' in string format and load it."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0.,  1.,  2.,  3.,  4.],\n",
       "       [ 5.,  6.,  7.,  8.,  9.]])"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = np.arange(10).reshape(2, 5)\n",
    "header = 'num1 num2 num3 num4 num5'\n",
    "np.savetxt('temp.txt', x, fmt=\"%d\", header=header) \n",
    "np.loadtxt('temp.txt')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Q4. Save `x`, `y`, and `z` to 'temp.txt' in string format line by line, then load it."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[  0.,   1.,   2.,   3.,   4.,   5.,   6.,   7.,   8.,   9.],\n",
       "       [ 11.,  12.,  13.,  14.,  15.,  16.,  17.,  18.,  19.,  20.],\n",
       "       [ 22.,  23.,  24.,  25.,  26.,  27.,  28.,  29.,  30.,  31.]])"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = np.arange(10)\n",
    "y = np.arange(11, 21)\n",
    "z = np.arange(22, 32)\n",
    "np.savetxt('temp.txt', (x, y, z), fmt='%d')\n",
    "np.loadtxt('temp.txt')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Q5. Convert `x` into bytes, and load it as array."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n"
     ]
    }
   ],
   "source": [
    "x = np.array([1, 2, 3, 4])\n",
    "x_bytes = x.tostring() # Don't be misled by the function name. What it really does is it returns bytes.\n",
    "x2 = np.fromstring(x_bytes, dtype=x.dtype) # returns a 1-D array even if x is not.\n",
    "print(np.array_equal(x, x2))\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Q6. Convert `a` into an ndarray and then convert it into a list again."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n"
     ]
    }
   ],
   "source": [
    "a = [[1, 2], [3, 4]]\n",
    "x = np.array(a)\n",
    "a2 = x.tolist()\n",
    "print(a == a2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## String formatting¶"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Q7. Convert `x` to a string, and revert it."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0 1 2 3 4]\n",
      " [5 6 7 8 9]] \n",
      " <class 'str'>\n"
     ]
    }
   ],
   "source": [
    "x = np.arange(10).reshape(2,5)\n",
    "x_str = np.array_str(x)\n",
    "print(x_str, \"\\n\", type(x_str))\n",
    "x_str = x_str.replace(\"[\", \"\") # [] must be stripped\n",
    "x_str = x_str.replace(\"]\", \"\")\n",
    "x2 = np.fromstring(x_str, dtype=x.dtype, sep=\" \").reshape(x.shape)\n",
    "assert np.array_equal(x, x2)\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Text formatting options"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Q8. Print `x` such that all elements are displayed with precision=1, no suppress."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0.5  0.   0.8  0.2  0.3  0.2  0.2  1.   0.4  0.8  0.6  0.2  0.5  0.1\n",
      "   0.4  0.1  0.9  0.6  0.1  0.5  0.8  0.8  0.8  0.   0.6  0.8  0.4  0.3\n",
      "   0.8  0.2  0.7  0.7  0.2  1.   0.8  0.1  0.2  0.1  0.3  0.1  0.5  0.9\n",
      "   0.6  0.9  0.6  0.5  0.8  0.3  0.3  0.5  0.1  0.6  0.1  0.3  0.6  0.2\n",
      "   0.4  0.8  0.6  0.4  0.2  0.6  0.   0.3  0.8  0.5  0.7  0.9  0.8  0.6\n",
      "   0.9  0.8  0.4  0.4  0.7  0.8  0.   0.1  0.5  0.4  0.7  1.   0.1  0.2\n",
      "   0.6  0.3  0.9  0.1  0.6  0.4  0.3  0.8  0.3  0.6  0.6  0.3  1.   0.2\n",
      "   0.9  0.2]\n",
      " [ 0.9  0.2  0.4  0.9  0.5  0.6  0.1  0.7  0.   0.   0.1  0.8  0.8  1.   0.2\n",
      "   0.8  0.3  0.2  1.   0.6  1.   0.3  0.4  0.4  0.7  0.5  0.4  0.8  0.5\n",
      "   0.9  0.3  0.5  0.7  0.4  0.2  0.3  0.9  0.   0.6  0.8  0.3  0.5  0.2\n",
      "   0.3  0.   0.6  0.5  0.2  0.5  0.8  0.2  0.8  0.   0.9  0.   0.7  0.1\n",
      "   0.4  0.2  0.5  0.6  0.2  0.6  0.1  0.1  0.   0.5  0.9  0.4  0.5  0.8\n",
      "   0.5  0.1  0.7  0.   1.   0.5  0.4  0.2  0.   1.   0.4  0.1  0.7  0.7\n",
      "   0.4  0.8  0.4  0.6  0.6  0.5  0.8  0.8  0.2  0.2  0.3  0.2  0.5  0.9\n",
      "   0.5]\n",
      " [ 0.3  0.6  0.4  0.5  0.5  0.   0.7  0.1  0.   0.9  0.5  0.7  0.6  0.3\n",
      "   0.9  0.5  0.1  0.4  0.1  0.9  0.8  0.6  0.8  0.8  0.1  0.4  0.9  0.1  1.\n",
      "   0.7  0.4  0.3  0.8  0.3  0.8  0.8  0.2  0.7  0.2  0.8  0.3  0.9  0.1\n",
      "   0.9  0.2  0.8  0.9  0.6  0.1  0.3  0.4  1.   0.1  0.7  0.3  0.9  0.3\n",
      "   0.5  0.9  0.   0.6  0.   0.8  0.1  0.9  0.   0.8  0.6  0.5  0.5  0.2  1.\n",
      "   0.4  0.   0.2  0.   0.9  0.9  0.8  0.2  0.7  0.3  0.2  0.1  1.   0.4\n",
      "   0.5  0.4  0.8  0.8  0.8  0.7  0.6  0.4  0.7  0.6  0.5  0.8  0.7  0.6]\n",
      " [ 0.2  0.6  0.9  0.7  0.1  0.1  1.   0.5  0.8  0.3  1.   0.4  0.1  0.5\n",
      "   0.6  0.8  0.8  0.8  0.1  1.   0.8  0.   0.7  0.6  0.8  0.2  0.5  0.9\n",
      "   0.4  0.8  0.7  0.2  0.8  0.6  0.9  0.6  0.9  0.8  0.9  1.   0.6  0.6\n",
      "   0.7  0.1  0.5  0.3  0.   0.8  0.   0.5  0.8  0.3  0.8  0.7  0.1  0.5\n",
      "   0.2  0.1  0.7  0.   0.   0.6  0.   0.8  0.7  0.1  0.4  0.1  0.2  0.1\n",
      "   0.9  0.6  0.9  0.3  0.4  0.9  0.2  0.6  0.8  0.9  0.6  0.8  0.5  0.1\n",
      "   0.6  1.   0.   0.7  0.7  0.4  0.1  0.9  0.4  0.1  0.7  0.6  0.3  0.9\n",
      "   0.3  0.5]\n",
      " [ 0.9  0.3  0.1  0.1  0.2  0.4  0.3  0.5  0.2  0.   0.5  0.4  0.5  0.3\n",
      "   0.6  1.   0.1  0.7  0.6  0.2  0.3  0.3  0.1  0.5  0.6  0.   0.6  0.7\n",
      "   0.6  0.4  0.2  0.6  0.1  0.9  0.9  0.1  0.9  0.1  0.6  0.6  0.   0.1\n",
      "   0.6  0.4  0.3  0.1  0.9  0.8  0.1  0.2  0.8  0.4  0.7  0.8  0.6  0.4\n",
      "   0.9  0.3  0.6  0.7  0.4  0.8  0.3  0.   0.   0.9  0.3  0.3  0.8  0.5\n",
      "   0.8  1.   0.2  0.6  0.6  0.2  0.2  0.2  0.4  0.6  0.6  0.4  0.4  0.8\n",
      "   0.2  0.5  0.7  0.7  0.1  0.9  0.5  0.6  0.3  0.3  0.6  0.8  0.6  0.8\n",
      "   0.4  0.3]\n",
      " [ 0.3  1.   0.6  0.9  0.6  1.   0.7  0.9  0.4  0.3  0.9  0.9  0.3  0.8\n",
      "   0.3  0.6  0.7  0.3  0.1  0.1  0.4  0.3  0.6  0.5  0.1  0.6  0.1  0.5\n",
      "   0.9  0.5  0.5  0.6  0.4  0.4  0.3  1.   0.6  0.6  0.3  0.1  0.4  0.7\n",
      "   0.7  0.1  0.5  0.1  0.3  0.1  0.6  0.7  0.   0.1  0.2  0.4  0.1  0.4\n",
      "   0.7  0.3  0.2  0.9  0.5  0.   0.4  0.9  1.   0.4  0.   0.2  0.3  0.9\n",
      "   0.3  0.   0.8  0.9  0.8  0.6  0.4  0.5  0.   0.9  0.6  0.6  0.1  0.6\n",
      "   0.9  0.1  0.8  0.6  0.6  0.5  0.7  1.   0.5  0.3  0.3  0.4  0.6  0.6  1.\n",
      "   0.2]\n",
      " [ 0.7  0.7  0.9  0.2  0.6  0.3  0.9  0.2  0.9  0.8  0.5  0.3  0.9  0.5  1.\n",
      "   0.6  0.9  0.5  0.5  0.1  0.8  0.3  0.9  0.5  0.7  1.   0.6  0.7  0.1\n",
      "   0.7  0.9  0.4  0.8  0.9  0.4  1.   0.1  1.   0.5  0.1  0.4  0.7  1.   0.4\n",
      "   0.3  0.2  0.2  0.6  0.6  0.3  0.7  0.5  0.7  0.1  0.3  0.5  1.   0.8\n",
      "   0.4  0.8  0.8  0.7  0.1  0.2  0.4  0.3  0.4  0.3  0.5  0.4  0.6  0.3\n",
      "   0.1  0.7  0.8  0.6  0.6  0.2  0.7  0.9  0.9  0.7  0.3  0.9  0.4  0.6  0.\n",
      "   0.4  0.4  0.2  0.8  0.3  0.1  0.2  0.6  0.5  0.9  0.8  0.9  0.7]\n",
      " [ 0.8  0.7  0.7  0.6  0.9  0.1  0.4  0.9  1.   0.3  0.   0.2  0.1  0.5\n",
      "   0.8  0.1  0.7  0.7  0.6  1.   0.7  1.   0.4  0.6  0.2  0.4  0.4  0.6  0.\n",
      "   0.1  1.   0.5  0.1  0.2  0.8  0.2  0.1  0.4  0.7  0.5  0.4  1.   0.5\n",
      "   0.5  0.4  0.8  0.2  0.1  0.7  0.2  0.1  0.4  0.3  0.6  0.9  0.9  0.9\n",
      "   0.9  0.1  0.1  0.   1.   0.   0.1  0.4  0.6  1.   0.4  0.9  0.3  0.2\n",
      "   0.7  0.   0.3  0.2  0.7  0.4  0.3  0.9  0.3  0.   0.5  0.2  0.3  0.1\n",
      "   0.2  0.   0.1  0.6  0.9  0.2  0.5  0.8  0.7  0.   0.4  0.8  0.8  0.5\n",
      "   0.2]\n",
      " [ 0.2  0.3  0.   0.1  0.8  0.4  0.1  0.2  0.   0.7  0.   1.   0.6  0.7\n",
      "   0.3  0.3  0.7  0.9  0.3  0.7  0.1  0.1  0.5  0.6  0.3  0.8  0.7  0.1\n",
      "   0.6  0.6  0.3  0.2  0.3  0.3  1.   0.1  0.1  0.2  0.4  0.4  0.6  0.5\n",
      "   0.7  0.7  0.2  0.   0.8  0.3  0.9  0.1  0.1  0.4  0.4  0.5  0.3  0.9\n",
      "   0.6  0.9  0.3  0.5  0.   0.4  0.8  1.   0.3  0.5  0.7  0.5  0.8  0.7\n",
      "   0.6  0.3  0.1  0.2  0.5  1.   0.9  0.5  0.6  0.6  0.2  0.8  0.6  0.   0.5\n",
      "   0.6  0.8  0.5  0.8  0.8  0.9  0.7  0.9  0.5  0.2  1.   1.   0.1  0.3\n",
      "   0.3]\n",
      " [ 0.   0.3  0.4  0.7  0.2  0.9  0.2  0.3  0.6  0.8  0.4  0.7  0.3  0.5\n",
      "   0.6  0.3  0.7  0.   0.1  0.1  0.9  0.   0.7  0.7  0.1  0.6  0.6  0.   0.3\n",
      "   0.5  0.9  0.3  0.1  0.3  0.1  0.9  0.6  0.3  0.3  0.4  0.4  0.2  0.3\n",
      "   0.1  0.5  0.3  0.8  0.   0.8  0.6  0.2  0.7  0.4  0.8  0.2  0.9  1.   1.\n",
      "   0.7  0.9  0.1  0.2  0.   0.5  0.8  0.7  0.6  0.7  0.7  0.5  0.9  0.2\n",
      "   0.2  0.1  0.2  0.1  0.7  1.   0.6  0.3  0.9  1.   0.3  0.3  0.7  0.9\n",
      "   0.5  0.8  0.9  0.7  0.2  0.7  0.3  0.1  0.9  0.2  0.5  0.6  0.3  0.4]]\n"
     ]
    }
   ],
   "source": [
    "x = np.random.uniform(size=[10,100])\n",
    "np.set_printoptions(precision=1, threshold=np.nan, suppress=True)\n",
    "print(x)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Base-n representations"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Q9. Convert 12 into a binary number in string format."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1100\n"
     ]
    }
   ],
   "source": [
    "out1 = np.binary_repr(12)\n",
    "out2 = np.base_repr(12, base=2)\n",
    "assert out1 == out2 # But out1 is better because it's much faster.\n",
    "print(out1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Q10. Convert 12 into a hexadecimal number in string format."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'44C'"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.base_repr(1100, base=16)"
   ]
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python [conda root]",
   "language": "python",
   "name": "conda-root-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
